package com.atung.parking.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.controller.DateConvertUtil;
import com.atung.parking.entity.BusMessagePublish;
import com.atung.parking.entity.BusParkingIotFranchise;
import com.atung.parking.enums.ExportType;
import com.atung.parking.enums.SuperUserState;
import com.atung.parking.export.FranchiseExportVO;
import com.atung.parking.export.PublicExportUtil;
import com.atung.parking.export.SuperAdminExportVO;
import com.atung.parking.mapper.BusParkingIotFranchiseMapper;
import com.atung.parking.mapstruct.ParkingIotFranchiseConverter;
import com.atung.parking.request.query.QueryFranchiseRequest;
import com.atung.parking.request.query.QueryMessageRequest;
import com.atung.parking.request.save.SaveOrUpdateFranchiseRequest;
import com.atung.parking.response.page.FranchiseResponse;
import com.atung.parking.service.ParkingIotFranchiseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wlt
 */
@Service
public class ParkingIotFranchiseServiceImpl implements ParkingIotFranchiseService {

    @Resource
    private BusParkingIotFranchiseMapper busParkingIotFranchiseMapper;
    @Resource
    private ParkingIotFranchiseConverter parkingIotFranchiseConverter;
    @Resource
    private PublicExportUtil publicExportUtil;

    @Override
    public ApiResult saveParkingIotFranchiseService(SaveOrUpdateFranchiseRequest request) {

        BusParkingIotFranchise busParkingIotFranchise=new BusParkingIotFranchise();
        BeanUtil.copyProperties(request,busParkingIotFranchise);
        busParkingIotFranchiseMapper.insert(busParkingIotFranchise);
        return ApiResult.ok();
    }

    @Override
    public void exportParkingIotFranchiseService(QueryFranchiseRequest request, HttpServletResponse httpServletResponse) {
        QueryWrapper<BusParkingIotFranchise> queryWrapper = createQueryWrapper(request);
        List<BusParkingIotFranchise> busParkingIotFranchises = busParkingIotFranchiseMapper.selectList(queryWrapper);
        List<FranchiseExportVO> collect = busParkingIotFranchises.stream().map(item -> {
            FranchiseExportVO franchiseExportVO = parkingIotFranchiseConverter.toExportVo(item);
            FastDateFormat formatter = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
            // 格式化日期时间
            try {
                String formattedDate = formatter.format(Long.valueOf(item.getCreatorTime()));
                franchiseExportVO.setCreatorTime(formattedDate);
            } catch (Exception e) {
                franchiseExportVO.setCreatorTime("时间格式化出现问题");
            }
            return franchiseExportVO;

        }).collect(Collectors.toList());
        try {
            publicExportUtil.exportReport(ExportType.PARKING_JOIN_EXPORT, collect, httpServletResponse);
        } catch (Exception e) {

        }
    }

    @Override
    public PageInfoResponse<BusParkingIotFranchise> queryParkingIotFranchiseService(QueryFranchiseRequest request) {
        QueryWrapper<BusParkingIotFranchise> queryWrapper = createQueryWrapper(request);
        Page<BusParkingIotFranchise> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<BusParkingIotFranchise> busParkingIotPage = busParkingIotFranchiseMapper.selectPage(page, queryWrapper);
        PageInfoResponse<BusParkingIotFranchise> pageResponse = new PageInfoResponse<>();
        pageResponse.setData(busParkingIotPage.getRecords());
        pageResponse.setTotal(busParkingIotPage.getTotal());
        return pageResponse;
    }

    @Override
    public List<FranchiseResponse> queryOneselfParkingIotFranchiseListService() {
        QueryWrapper<BusParkingIotFranchise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creator_id", AuthUserMangerContext.getUserId());
        queryWrapper.orderByDesc("creator_time");
        List<BusParkingIotFranchise> busParkingIotFranchises = busParkingIotFranchiseMapper.selectList(queryWrapper);

      return  busParkingIotFranchises.stream().map(parkingIotFranchiseConverter::toResponse).collect(Collectors.toList());

    }

    public QueryWrapper<BusParkingIotFranchise> createQueryWrapper(QueryFranchiseRequest request) {
        QueryWrapper<BusParkingIotFranchise> queryWrapper = new QueryWrapper<>();
        //联系电话
        if (StringUtils.hasText(request.getContactPhone())) {
            queryWrapper.eq("contact_phone", request.getContactPhone());
        }

        //联系人
        if (StringUtils.hasText(request.getContactPerson())) {
            queryWrapper.eq("contact_person", request.getContactPerson());
        }
        //城市
        if (StringUtils.hasText(request.getCity())) {
            queryWrapper.eq("city", request.getCity());
        }
        //车场名称
        if (StringUtils.hasText(request.getParkingLotName())) {
            queryWrapper.eq("parking_iot_name", request.getParkingLotName());
        }
        //时间查询
        if (StringUtils.hasText(request.getStartData()) && StringUtils.hasText(request.getEndData())) {
            Long start = DateConvertUtil.convertQueryStartToMillis(request.getStartData());
            Long end = DateConvertUtil.convertQueryEndToMillis(request.getEndData());
            if (Objects.nonNull(request.getStartData()) && Objects.nonNull(request.getEndData())) {
                queryWrapper.between("creator_time", start, end);
            }

        }
        queryWrapper.orderByDesc("creator_time");
        return queryWrapper;
    }
}
